package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.TrainingTag;
import com.think.teaching.service.impl.TrainingTagServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 实训标签 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "实训标签管理接口")
public class TrainingTagController {
	
	private final TrainingTagServiceImpl trainingTagService;

	public TrainingTagController(TrainingTagServiceImpl trainingTagService) {
		this.trainingTagService = trainingTagService;
	}

	@ApiOperation("添加新的实训标签")
	@PostMapping("admin/trainingTagTag")
	public Result AddNewTrainingTagTag(@RequestBody()TrainingTag data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingTag trainingTag = trainingTagService.AddTrainingTag(data);
			if (trainingTag != null) {
				return Result.success(trainingTag);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新实训标签")
	@RequestMapping(value = "admin/trainingTagTag",method = RequestMethod.PUT)
	public Result UpdateTrainingTagTag(@RequestBody()TrainingTag data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingTag trainingTag = trainingTagService.UpdateTrainingTag(data);
			if (trainingTag != null) {
				return Result.success(trainingTag);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除实训标签")
	@RequestMapping(value = "admin/trainingTagTag/{id}",method = RequestMethod.DELETE)
	public Result DeleteTrainingTagTag(@PathVariable("id")Integer id) {
		TrainingTag trainingTag = trainingTagService.getById(id);
		if (trainingTag == null) {
			return Result.failure(ResultCode.TRAINING_TAG_NOT_EXIST);
		} else {
			if (trainingTagService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取实训标签详情")
	@RequestMapping(value = "admin/trainingTagTag/{id}",method = RequestMethod.GET)
	public Result GetTrainingTagTag(@PathVariable("id")Integer id) {
		TrainingTag trainingTag = trainingTagService.getById(id);
		if (trainingTag == null) {
			return Result.failure(ResultCode.TRAINING_TAG_NOT_EXIST);
		} else {

			return Result.success(trainingTag);
		}
	}
	
	@ApiOperation("管理员修改实训标签状态")
	@RequestMapping(value = "admin/trainingTagTag/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateTrainingTagTagState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		TrainingTag trainingTag = trainingTagService.getById(id);
		if (trainingTag == null) {
			return Result.failure(ResultCode.TRAINING_TAG_NOT_EXIST);
		} else {
			try {
				trainingTag = trainingTagService.SetTrainingTagState(id, state);
				return Result.success(trainingTag);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	
	@ApiOperation("管理员搜索实训标签列表")
	@RequestMapping(value = "admin/trainingTagTags/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListTrainingTagTag(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(trainingTagService.ListTrainingTag(page, count, false));
	}
	
	@ApiOperation("用户搜索实训标签列表")
	@RequestMapping(value = "user/trainingTagTags/{page}/{count}",method = RequestMethod.GET)
	public Result UserListTrainingTagTag(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(trainingTagService.ListTrainingTag(page, count, true));
	}
}

